<template>
  <div class="app-container page-wood-purchase-record">
    <FilterForm :data="filterData" @search="getList" labelWidth="86px" />
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:salesDelivery:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:salesDelivery:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
         <el-button
          type="warning"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleCheckOrder(1)"
          v-hasPermi="['business:salesDelivery:edit']"
        >审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleCheckOrder(0)"
          v-hasPermi="['business:salesDelivery:edit']"
        >反审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <Export
          :api="$Api.sales.delivery.export"
        />
      </el-col>
    </el-row>

    <Table
        :api="$Api.sales.delivery.list"
        :selection="true"
        @select="handleSelectionChange"
        @dbClick="rowDblclick"
        ref="Table"
        height="60vh"
        >
      <el-table-column label="操作" align="center" width="130"   fixed="left">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click.native.stop="handleUpdate(scope.row)"
            v-hasPermi="['business:salesDelivery:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click.native.stop="handleDelete(scope.row)"
            v-hasPermi="['business:salesDelivery:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="单据日期" align="center" prop="billTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.billTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <div>
        
      </div>
      
      <el-table-column label="客户" align="center" prop="customerName" width="120"/>
      <el-table-column label="品名" align="center" prop="goodsName" />
      <el-table-column label="品牌" align="center" prop="brandName" />
      <el-table-column label="仓库" align="center" prop="warehouse" />
      <el-table-column label="方数" align="center" prop="salesCount" />
      <el-table-column label="金额" align="center" prop="money" />
      <el-table-column label="客户承担费用" align="center" prop="postFee" width="155" />
      <el-table-column label="折扣" align="center" prop="rebate" />
      <el-table-column label="已收款" align="center" prop="receivedMoney" />
      <el-table-column label="收款状态" align="center" key="recStatus" >
        <template slot-scope="scope">
          <span v-if="scope.row.recStatus == 0">未收款</span>
          <span v-else-if="scope.row.recStatus == 1">部分收款</span> 
          <span v-else>全部收款</span> 
        </template>
      </el-table-column>
      <el-table-column label="车号" align="center" prop="carNo" />
      <el-table-column label="销售人员" align="center" prop="salesName" width="160"/>
      <el-table-column label="单据编号" align="center" prop="deliveryId" width="160"/>
      <el-table-column label="关联销售单号" align="center" prop="salesId" width="155"/>
      <el-table-column label="制单人" align="center" prop="createBy" />
      <el-table-column label="审核人" align="center" prop="auditor" fixed="right"/>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="审核状态" align="center" key="auditStatus" fixed="right">
        <template slot-scope="scope">
         <span class="success" v-if="scope.row.auditStatus == 1">已审核</span>
         <span class="error" v-else>未审核</span>
        </template>
      </el-table-column>
      <el-table-column label="开票状态" align="center" key="billingStatus">
        <template slot-scope="scope">
         <span class="success" v-if="scope.row.billingStatus == 1">未开票</span>
         <span class="error" v-else>已开票</span>
        </template>
      </el-table-column>
      <el-table-column label="开票备注" align="center" prop="remark" />
    </Table>

  </div>
</template>

<script>

export default {
  name: "PurchaseOrder",
    components: {
  },
  data() {
    return {
      // 选中数组
      ids: [],
      // 选中状态
      status:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      filterData: [
                {
                    label: "单据日期",
                    type: "period_date",
                    st: "beginRegisterTime",
                    end: "endRegisterTime",
                    scale: 2,
                },
                {
                    label: "客户",
                    name: "customerName",
                    type: "customer",
                },
                { label: "单据编号", name: "deliveryId" },
                {
                    label: "品名",
                    name: "goodsName",
                    type: "select",
                    select: "goodsName",
                },
                {
                    label: "仓库",
                    name: "warehouse",
                    type: "select",
                    select: "warehouse",
                },
                {
                    label: "审核状态",
                    name: "auditStatus",
                    type: "select",
                    select:{
                      option:[{
                              value: '1',
                              label: '已审核'
                            }, {
                              value: '0',
                              label: '未审核'
                            }
                          ],
                        selectLabel: "label",
                        selectValue: "value",
                        multiple:true
                    },
                },
                {
                    label: "订单状态",
                    name: "orderStatus",
                    type: "select",
                    select: {
                        option: [{
                            value: '0',
                            label: '未收款'
                          }, {
                            value: '1',
                            label: '部分收款'
                          }, {
                            value: '2',
                            label: '全部收款'
                          }],
                        selectLabel: "label",
                        selectValue: "value",
                    },
                },
            ],
  
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        delFlag: [
          { required: true, message: "删除标志;不能为空", trigger: "blur" }
        ],
      }
    };
  },
  computed:{

  },
  created() {
    
  },
  methods: {
    /** 查询采购订单信息列表 */
    getList(params) {
            this.$refs.Table.getData(params);
        },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.deliveryId)
      this.status = selection.map(item => item.auditStatus)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
       this.$router.push({
        path: "/sale/receipts/outbound",
      });
    },
    // 双击操作
    rowDblclick(row, column, event) {
      this.handleUpdate(row);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.deliveryId;
      let path = `/sales/salesDelivery/list/${id}`;
      this.$router.push({
        path,
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let state = false
      let { status } = this
      for( let i = 0; i < status.length; i++) {
        if( status[i] == 1) {
          state = true
        }
      }
      if(row.auditStatus == 1 || state == true) {
        this.$message.error('已审核，不能删除');
      }else {
        const deliveryIds = row.deliveryId || this.ids;
        this.$confirm('是否确认删除采购订单信息编号为"' + deliveryIds + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() =>{
             this.$Api.sales.delivery.del(deliveryIds).then( (res) =>{
                 let { code } = res;
                 if( code == 200){
                    this.getList();
                    this.msgSuccess("删除成功");
                 }
             })
          }).catch(() => {});
        }
    },
    // 审核订单 审核与反审核 审核状态;0未审核 1已审核
    handleCheckOrder(isStatus){
      let { ids } = this;
      this.$confirm('是否确认'+`${isStatus == "1" ? '审核' : '反审核'}`+'采购订单信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.$Api.sales.delivery.checkStatus({
            ids: ids,
            auditStatus:isStatus
        }).then( (res) =>{
          if(res.code == 200){
              this.getList();
              this.msgSuccess( `${isStatus == "1" ? '审核' : '反审核'}`+"成功");
          }
        }).catch(() => {
           this.getList();
        });  
        })
    },
  }
};
</script>

<style lang="scss" scoped>
  .page-wood-purchase-record {
  .success{
    color: #67C23A;
  }
  .error{
    color: #F56C6C;
  }
  .disabled {
    color: #a8a8a8;
  }

  .error {
    color: #fa3e3e;
  }

}
</style>

